using System;
using System.Collections.Generic;
using System.Text;

namespace MindFactorial.LaunchPoint.Common
{
    /// <summary>
    /// represents a generic dictionary of named objects
    /// </summary>
    /// <typeparam name="K"></typeparam>
    /// <typeparam name="V"></typeparam>
    public abstract class GenericNamedObjectDictionary<K, V> : ImmortalIPCObject, IModifyableCollection<K, V> 
        where V : INamedObject<K> 
    {
        /// <summary>
        /// the internal collection
        /// </summary>
        protected Dictionary<K, V> _items = new Dictionary<K, V>();

        /// <summary>
        /// indexes the internal items
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public virtual V this[K key]
        {
            get
            {
                if (_items.ContainsKey(key))
                    return _items[key];
                else
                    return default(V);
            }
        }

        /// <summary>
        /// determines whether the dictionary has the given key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool ContainsKey(K key)
        {
            return _items.ContainsKey(key);
        }

        /// <summary>
        /// Gets an enumerator that can be used to iterate through the collection
        /// </summary>
        /// <returns></returns>
        public System.Collections.IEnumerator GetEnumerator()
        {
            return _items.Values.GetEnumerator();
        }

        /// <summary>
        /// adds the specified item tot he collection
        /// </summary>
        /// <param name="item"></param>
        public void Add(V item)
        {
            Add(item.Name, item);
        }

        /// <summary>
        /// adds the specified item with a given name.  The regular add version uses the
        /// nameditems name 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="item"></param>
        public void Add(K key, V item)
        {
            if (!_items.ContainsKey(key))
                _items.Add(key, item);
            else
                _items[key] = item;
        }

        /// <summary>
        /// removes the nameditem
        /// </summary>
        /// <param name="key"></param>
        public void Remove(K key)
        {
            _items.Remove(key);
        }


    }
}
